<?php

// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------

namespace app\adminapi\logic\travel;


use app\common\model\travel\Line;
use app\common\logic\BaseLogic;
use app\common\model\travel\LineStatusLog;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Db;


/**
 * Line逻辑
 * Class LineLogic
 * @package app\adminapi\logic\travel
 */
class LineLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/25 10:24
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            Line::create([
                'travel_agency_id' => $params['travel_agency_id'],
                'user_id' => $params['user_id'],
                'title' => $params['title'],
                'start_address' => $params['start_address'],
                'end_address' => $params['end_address'],
                'line_image' => $params['line_image'],
                'desc' => $params['desc'],
                'content' => $params['content'],
                'is_hot' => $params['is_hot'],
                'tags' => $params['tags'],
                'sort' => $params['sort'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/25 10:24
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Line::where('id', $params['id'])->update([
                'travel_agency_id' => $params['travel_agency_id'],
                'user_id' => $params['user_id'],
                'title' => $params['title'],
                'start_address' => $params['start_address'],
                'end_address' => $params['end_address'],
                'line_image' => $params['line_image'],
                'desc' => $params['desc'],
                'content' => $params['content'],
                'is_hot' => $params['is_hot'],
                'tags' => $params['tags'],
                'sort' => $params['sort'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/25 10:24
     */
    public static function delete(array $params): bool
    {
        return Line::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/25 10:24
     */
    public static function detail($params): array
    {
        return Line::findOrEmpty($params['id'])->append(['travel_agency_name'])->toArray();
    }

    /**
     * @notes  更改状态
     * @param array $params
     * @return bool
     */
    public static function updateStatus(array $params)
    {
        Line::update([
            'id' => $params['id'],
            'status' => $params['status']
        ]);

        LineStatusLog::create([
            'line_id' => $params['id'],
            'user_id' => $params['user_id'],
            'status' => $params['status'],
            'remark' => $params['comment']
        ]);
        return true;
    }

    public static function import($file, $travelAgencyId, $userId)
    {
        try {
            // 加载 Excel 文件
            $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file->getRealPath());
            $objReader = IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file->getRealPath());

            // 获取第一个工作表
            $sheet = $objPHPExcel->getActiveSheet();
            $rows = $sheet->toArray();

            // 去除表头
            array_shift($rows);


            foreach ($rows as $row) {
                $data = [
                    'travel_agency_id' => $travelAgencyId,
                    'user_id' => $userId,
                    'title' => trim($row[0]),
                    'start_address' => trim($row[1]),
                    'end_address' => trim($row[2]),
                    'desc' => trim($row[3]),
                    'line_image' => trim($row[7] ?? ''),
                    'content' => trim($row[8] ?? ''),
                    'is_hot' => trim($row[9] ?? ''),
                    'sort' => trim($row[10] ?? ''),
                ];
                if (!self::add($data)) {
                    return false;
                }
            }

            return true;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

}